<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/static/lib/layui/css/layui.css">
</head>

<body>
  <div>
    <span class="layui-breadcrumb">
      <a href="/dashBoard">首页</a>
      <a><cite>商品管理</cite></a>
    </span>
  </div>
  <a style="margin-top:30px" href="/itemAdd" class="layui-btn layui-btn-warm">增加商品</a>
  <table class="layui-table layui-form">
    <thead>
      <tr>
        <th>商品名称</th>
        <th>商品价格</th>
        <th>描述</th>
        <th>上架时间</th>
        <th>上下架</th>
        <th>封面图片</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <% for(var i=0;i<items.length; i++) { %>

        <tr>
          <td>
            <%= items[i].name %>
          </td>
          <td>
            <%= items[i].price %>
          </td>
          <td title="<%= items[i].desc %>">
            <%= items[i].desc ? items[i].desc.length>10
              ?
              items[i].desc.substr(0, 10) + '...'
              :
              items[i].desc
              :
              '暂无描述'
              %>
          </td>
          <td>
            <%= items[i].createAt %>
          </td>
          <td>
            <input type="checkbox" name="zzz" disabled <%=items[i].onoff?'checked':'' %> lay-skin="switch"
            lay-text="上架|下架">
          </td>
          <td>
            <img
              src="<%= items[i].thumb?items[i].thumb :'https://img1.baidu.com/it/u=3975844624,143306360&fm=253&fmt=auto&app=138&f=JPEG?w=281&h=500' %>"
              width="100" alt="">
          </td>
          <td>
            <a href="/itemUpdate?_id=<%= items[i]._id %>" class="layui-btn layui-btn-xs">编辑</a>
            <button data-id="<%= items[i]._id %>" class="layui-btn del layui-btn-danger layui-btn-xs">删除</button>
          </td>
        </tr>
        <% } %>
    </tbody>
  </table>
  <div id="page"></div>

  <script src="/static/lib/layui/layui.js"></script>
  <script>
    layui.use(['laypage', 'jquery', 'layer'], function () {
      var laypage = layui.laypage;
      var $ = layui.jquery;
      var layer = layui.layer;
      // 删除商品
      $('.del').click(function () {
        // 弹出确认框
        var _id = this.dataset.id;
        layer.confirm('您确认删除该商品吗?', { icon: 3, title: '删除' }, function (index) {
          //do something
          $.ajax({
            url: '/delItem',
            data: {
              _id: _id
            },
            success: function (res) {
              if (res.code === 0) {
                // 成功
                layer.msg(res.msg, {
                  icon: 1,
                  time: 2000
                }, function () {
                  history.go(0);
                })
              } else {
                // 失败
                layer.msg(res.msg, {
                  icon: 2,
                  time: 2000
                })
              }
            }
          })
          layer.close(index);
        });
      })
      // 渲染分页
      laypage.render({
        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
        // 当前页 
        , curr: <%= page %>
        , count: <%= counts %> //数据总数，从服务端得到
        , limit: 2
        , jump: function (obj, first) {

          if (!first) {
            location.href = "/itemLists?page=" + obj.curr + "&pageSize=" + obj.limit;
          }
        }
      });
    })
  </script>
</body>

</html>